using System.Net;
using System.Xml;
using nTribeHR.Utilities;

namespace nTribeHR
{
    public class TribeHRServiceBase
    {
        public DisposableStopwatch WebRequestStopwatch { get; set; }
        public DisposableStopwatch ParseResultStopwatch { get; set; }
        public DisposableStopwatch LoadXmlStopwatch { get; set; }

        protected TribeHRConnection tribeHRConnection { get; set; }

        protected XmlDocument LoadXmlDocumentforUrl(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tribeHRConnection.ConnectUrl + url);
            request.Credentials = new NetworkCredential(tribeHRConnection.Username, tribeHRConnection.ApiKey);

            using (WebRequestStopwatch = DisposableStopwatch.StartNew("Web Request"))
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    WebRequestStopwatch.Stop();

                    XmlDocument xdoc = new XmlDocument();
                    using (LoadXmlStopwatch = DisposableStopwatch.StartNew("Load XML"))
                    {
                        if(response.ContentLength > 0)
                        {
                            xdoc.Load(response.GetResponseStream());
                        }
                    }

                    return xdoc;
                }
            }
        }

    }
}